//
//  WYInsertHistoryController.m
//  Boss
//
//  Created by admin on 2018/8/20.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYInsertHistoryController.h"
#import "WYDynamicCell.h"
#import "WYMYDynamic.h"
@interface WYInsertHistoryController ()<UITableViewDataSource,UITableViewDelegate,WYHonorCellDelegate>
{
    BOOL _isRefresh;
}
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,assign)NSUInteger page;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation WYInsertHistoryController
-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    //    self.tableView.backgroundColor = [UIColor purpleColor];
    [self setupUI];
    [self loadData];
    
    
    WEAKSELF
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _isRefresh=NO;
        weakSelf.page++;
        // [weakSelf loadTableData];
        [weakSelf loadData];
    }];
}
-(void)setupUI
{
    self.navigationItem.title = @"历史信息导入";
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYDynamicCell" bundle:nil] forCellReuseIdentifier:@"WYDynamicCell"];
    self.tableView.estimatedRowHeight = 300;
}
-(void)loadData
{
    NSString *page = [NSString stringWithFormat:@"%ld",self.page];
    WEAKSELF
    [WYHttpServer getMyDynamicWithCurrentPage:page WithPageSize:@"15" SucessBlock:^(BOOL success, id response, NSString *errorMessage) {
        
        if (success) {
            if (_isRefresh) {
                [weakSelf.dataArray removeAllObjects];
            }
            
            
            NSArray *array = [WYMYDynamic getDynamicArrayFromJson:response[@"result"][@"detail"]];
            [weakSelf.dataArray addObjectsFromArray:array];
            [weakSelf.tableView reloadData];
            if ([response[@"result"][@"hasNextPage"] integerValue]) {
                [weakSelf.tableView.mj_footer endRefreshing];
            }else{
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WYDynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYDynamicCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"WYDynamicCell" owner:nil options:nil].firstObject;
    }
    cell.delegate = self;
    WYMYDynamic *dyc = self.dataArray[indexPath.row];
    [cell setSelectionStyle:(UITableViewCellSelectionStyleNone)];
    [cell setHistory:dyc];
    
    //cell.selfVc = self.selfVC;
    return cell;
}
-(void)deleteActionWithDymic:(WYMYDynamic *)dyc
{
    if (self.complte) {
        self.complte(dyc);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
    
}
-(UITableView *)tableView
{
    if (!_tableView) {
        
        CGRect frame = CGRectMake(0, self.navHeight, kScreenWidth, kScreenHeight-self.navHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}

@end
